import com.dmotiko.akira.*

class PhotoPreloader {
	
	private static var mcl:MovieClipLoader;
		
	public static function run(){
		//creo un clip que va a contener a todas las imagenes
		var clip:MovieClip = _root.createEmptyMovieClip("_photoPreloader", _root.getNextHighestDepth());
		clip.createEmptyMovieClip("img", 1);
		
		mcl = new MovieClipLoader();
		mcl.addListener(_root._photoPreloader);
		
		//meto todas las imagenes en un array
		var aWorks:Array = AkiraApp.getApp().getWorks();
		var aPhotos:Array = new Array();
		for (var i:Number = 0; i < aWorks.length; i++) {
			for (var i2:Number = 0; i2 < aWorks[i].photos.length; i2++) {
				aPhotos.push(aWorks[i].photos[i2]);
			}
		}
		
		//hago un "iterador asincronico"
		var nPhoto:Number = 0;
		clip.loadNextPhoto = function(){
			if(nPhoto == aPhotos.length) {
				this.img.unloadMovie();
				this.removeMovieClip();
				return;
			}
			mcl.loadClip(aPhotos[nPhoto].photo, clip.img);
		}
		clip.onLoadInit = function(target){
			target._visible = false;
			nPhoto ++;
			this.loadNextPhoto();
			
		}
		clip.loadNextPhoto();
	}
	
}